package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import singleThreaded.MainSingle;
import MultiThreaded.MultiThreadSetup;
/**
 * CrawlingFrame: the frame and the component of the frame.
 * @author zsyed
 *
 */
@SuppressWarnings("serial")
public class CrawlingFrame extends JFrame{
	/**the frame width.*/
	private static int WIDTH = 1200;

	/**the frame height.*/
	private static int HEIGHT = 600;

	/** the panel. */
	private JPanel my_panel; 

	/** constructing the crawling frame.*/
	public CrawlingFrame() {
		my_panel = new JPanel();
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		setTitle("Web Crawling");
		setLayout(null);
	}
	/** set up the frame with buttons and text field */
	public void start() {
		my_panel.setLayout(null);
		my_panel.setBackground(Color.GRAY);
		my_panel.setBounds(0, 0, WIDTH, HEIGHT);
		ButtonGroup group = new ButtonGroup();
		JLabel url = new JLabel("URL:");
		url.setBounds(25, 25, 100, 25);
		final JTextField starting_url = new JTextField();
		starting_url.setBounds(125, 25, 400, 25);
		JLabel key_warning = new JLabel("A maximum of 10 keywords separated by spaces");
		key_warning.setBounds(125, 50, 400, 25);
		JLabel key = new JLabel("Keywords:");
		key.setBounds(25, 75, 200, 25);
		final JTextField keywords = new JTextField();
		keywords.setBounds(125, 75, 400, 25);
		JLabel crawl_label = new JLabel("Maximum Pages: ");
		crawl_label.setBounds(25, 115, 200, 25);
		final JTextField max_pages = new JTextField("14");
		max_pages.setBounds(150, 115, 100, 25);
		final JRadioButton single = new JRadioButton("Single-Threaded");
		single.setBounds(25, 140, 175, 25);
		//set default to single threaded.
		single.setSelected(true);
		final JRadioButton multip = new JRadioButton("Multithreaded");
		multip.setBounds(225, 140, 175, 25);
		JLabel input_label = new JLabel("Input url:");
		input_label.setBounds(25, 175, 100, 25);
		final JTextArea input_area = new JTextArea();
		input_area.setEditable(false);
		JScrollPane input_scroll = new JScrollPane(input_area);
		input_scroll.setBounds(25, 200, 500, 50);
		JLabel output_label = new JLabel("Output:");
		output_label.setBounds(25, 250, 100, 25);
		final JTextArea output_area = new JTextArea();
		output_area.setEditable(false);
		JScrollPane output_scroll = new JScrollPane(output_area);
		output_scroll.setBounds(25, 275, 500, 275);
		JButton search = new JButton("Search");
		search.setBounds(300, 115, 100, 25);
		search.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				boolean runnable = false;
				long total_start_time = System.nanoTime();
				if (max_pages.getText().equals("") || starting_url.getText().equals("") ||
						keywords.getText().equals("")) {
					JOptionPane.showMessageDialog(null, "Please enter the keywords");				
				}
				else {
					int max = 0;
					try {
						max = Integer.parseInt(max_pages.getText());
						if (max !=0) {
							if (max < 99) {
								runnable = true;
							}
						}else {
								JOptionPane.showMessageDialog(null, "Maximum page is 14 and not 0");
							
						}
					}
					catch(NumberFormatException the_exeption)
					{
						JOptionPane.showMessageDialog(null, "Maximum page needs to be a number");	
					}
					if (single.isSelected() && runnable) {
						DecimalFormat df = new DecimalFormat("#.##");
						//single thread main.
						MainSingle main_single = new MainSingle(max, starting_url.getText(), keywords.getText());
						main_single.Crawl();
						input_area.setText(main_single.getStartingURL());
						output_area.setText(main_single.getOutPut());
						double total_end_time = System.nanoTime() - total_start_time;
						// convert to sec and append it to the text output.
						total_end_time = total_end_time / Math.pow(10, 9);
						String format = df.format(total_end_time);
						output_area.append("Total running time : " + format + " sec");
						JOptionPane.showMessageDialog(null, "Crawling Completed");
					}
					else if (multip.isSelected() && runnable){
						total_start_time = System.nanoTime();
						//multi thread main.
						MultiThreadSetup main = new MultiThreadSetup(max, starting_url.getText(), keywords.getText());
						main.Crawl();
						long end = System.nanoTime();
						input_area.setText(main.getURLstart());
						StringBuilder string = new StringBuilder();
						DecimalFormat df = new DecimalFormat("#.##");
						string.append("Pages Retrieved: " + main.getPagesRetrieved());
						string.append("\nAverage words per page: " + df.format(main.getAvgWordsPerPage()));
						string.append("\nAverage URLs per page: " + df.format(main.getAvgUrlsPerPage()));
						string.append("\nKeyword\tAvg.Hits\tTotal hits");
						int i = 0;
						for (String key : main.getKeywords().keySet()) {
							string.append("\n");
							string.append(key);
							string.append("\t");
							string.append(df.format(main.getKeywordsAvgHits(i)));
							string.append("\t");
							string.append(main.getKeywords().get(key));
							i++;
						}
						string.append("\nAverage parse time per page: " + df.format(main.getAvgParseTime()) + " sec");
						string.append("\nTotal running time: " + df.format(((end-total_start_time)/Math.pow(10, 9))) + " sec");
						output_area.setText(string.toString());
						JOptionPane.showMessageDialog(null, "Crawling Completed");
					}

				}
			}
		});
		my_panel.add(url);
		my_panel.add(key_warning);
		my_panel.add(starting_url);
		my_panel.add(key);
		my_panel.add(keywords);
		my_panel.add(crawl_label);
		my_panel.add(max_pages);
		my_panel.add(search);
		group.add(single);
		group.add(multip);
		my_panel.add(single);
		my_panel.add(multip);
		my_panel.add(input_label);
		my_panel.add(input_scroll);
		my_panel.add(output_label);
		my_panel.add(output_scroll);
		add(my_panel);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		pack();
	}
}
